package com.example.timmy.myapplication.ui.activity;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.timmy.myapplication.R;
import com.example.timmy.myapplication.adapter.NotifyItemAdapter;
import com.example.timmy.myapplication.model.MultipleItem;
import com.example.timmy.myapplication.utils.TimeUtils;

import java.util.ArrayList;
import java.util.List;

public class NotifyItemActivity extends AppCompatActivity implements View.OnClickListener {

    private RecyclerView mRecyclerView;
    private NotifyItemAdapter mItemAdapter;
    private List<Integer> mIntegerList;
    private Handler mHandler;
    private TimeUtils mTimeUtils;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notify_item);

        initView();
        initData();
    }

    private void initView() {
        findViewById(R.id.btn_start).setOnClickListener(this);
        findViewById(R.id.btn_correct).setOnClickListener(this);
        findViewById(R.id.btn_error).setOnClickListener(this);

        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerview);
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

        mItemAdapter = new NotifyItemAdapter();

        mItemAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mItemAdapter.isFirstOnly(false);
        mRecyclerView.setAdapter(mItemAdapter);
    }

    private void initData() {
        mIntegerList = new ArrayList<>();
        mIntegerList.add(0);
        mHandler = new Handler();

        mTimeUtils = new TimeUtils();
        mTimeUtils.setTimeListener(mTimeListener);
    }

    TimeUtils.TimeListener mTimeListener = new TimeUtils.TimeListener() {
        @Override
        public void onTimeChange(long totalCurrTime, long deputyCurrTime) {

            int itemCount = mItemAdapter.getItemCount() - 1;
            MultipleItem item = mItemAdapter.getItem(itemCount);

            mItemAdapter.setData(itemCount, new MultipleItem(item.getItemType(), (int) totalCurrTime));
        }
    };

    @Override
    public void onClick(View view) {            // []    S
        switch (view.getId()) {
            case R.id.btn_start:
                mItemAdapter.addData(new MultipleItem(MultipleItem.TYPE_CORRENT, 0));
                mTimeUtils.initTime();
                break;
            case R.id.btn_correct:
                MultipleItem itemCorrent = mItemAdapter.getItem(mItemAdapter.getItemCount() - 1);
                mItemAdapter.addData(new MultipleItem(MultipleItem.TYPE_CORRENT, itemCorrent.getCount()));
                break;
            case R.id.btn_error:
                MultipleItem itemError = mItemAdapter.getItem(mItemAdapter.getItemCount() - 1);
                mItemAdapter.addData(new MultipleItem(MultipleItem.TYPE_ERROR, itemError.getCount()));
                break;
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        mTimeUtils.stopTimer();
    }
}
